package com.zx.club.wx.mp.controller;

import com.zx.club.common.entity.BaseResp;
import com.zx.club.wx.mp.utils.WxMpQrCodeCacheUtils;
import jakarta.annotation.Resource;
import me.chanjar.weixin.mp.api.WxMpQrcodeService;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 微信公众号扫码控制类
 *
 * @author zx
 * @date 2025-03-20 14:16
 */
@RestController
@RequestMapping("/wx/mp")
public class WxQrCodeController {

    @Resource
    private WxMpService wxMpService;


    /**
     * @return 生成临时二维码请求连接
     * @Desc 生成临时二维码
     */
    @GetMapping("/qrCode")
    public BaseResp<String> getQrCode() {
        String qrCodeUrl = null;
        try {
            WxMpQrcodeService qrcodeService = wxMpService.getQrcodeService();
            WxMpQrCodeTicket mpScanLogin = qrcodeService.qrCodeCreateTmpTicket("mp_scan_login", 7200);
            String ticket = mpScanLogin.getTicket();
            qrCodeUrl = qrcodeService.qrCodePictureUrl(ticket);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return BaseResp.success(qrCodeUrl);
    }


    /**
     * @param ticket 二维码ticket
     * @return true 表示用户扫码关注; false 表示没有扫码关注
     * @Desc 判断用户是否扫码
     */
    @GetMapping("/isScan")
    public BaseResp<Boolean> isScan(String ticket) {
        boolean isScan = !StringUtils.isBlank(WxMpQrCodeCacheUtils.getOrDefault(ticket));
        return BaseResp.success(isScan);

    }

    @GetMapping("/getOpenId")
    public BaseResp<String> getOpenId(String ticket) {
        return BaseResp.success(WxMpQrCodeCacheUtils.getOrDefault(ticket));
    }


}
